package exercise;

import java.util.Arrays;

class Solution5 {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int res = 0, len = nums.length;

        for (int i = len-1; i >= 2; i--) {
            int l = 0, r = i - 1;
            while (l < r) {
                if (nums[l] + nums[r] > nums[i]) {
                    res += r - l;
                    r--;
                } else {
                    l++;
                }
            }
        }

        return res; 
    }
}